<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<!DOCTYPE html>
<html>
<head>
	<title>添加分录</title>
</head>
	<body>	
		<script type="text/javascript">
			
			var products;
			$.ajax({
				 	 type: "post",   
				     url: "${pageContext.request.contextPath}/business/accounting/xYRJZ/hsdw.shtml",
				 	 datatype: "json",
				 	 success:function(data){
				 	 	products = JSON.parse(data);
				     }
			});
			
			var kmbm;
			$.ajax({
				 	 type: "post",   
				     url: "${pageContext.request.contextPath}/business/accounting/xYRJZ/kmbm.shtml",
				 	 datatype: "json",
				 	 success:function(data){
				 	 	kmbm = JSON.parse(data);
				     }
			});
			
			$(function() {
				var psel = document.getElementById("XSPZH");
				griddata(psel.value);
			});
			
			var dataGrid;
			function griddata(xspzh){
				dataGrid = $('#dataGridxyfl').edatagrid({
					url : '${pageContext.request.contextPath}/business/accounting/xYRJZ/jzpzdataGrid.shtml?xspzh='+xspzh,
					rownumbers : true,
					singleSelect : true,
					columns : [[
					         
					        {field : 'ID',title : 'ID',align : 'center',align : 'center' , hidden : true},
							{field: 'HSDW', title: '核算单位', hidden : true},
							{field: 'TITLE', title: '核算单位', width: 150, editor:{type:'combobox',options:{valueField:'ID',textField:'TITLE',data:products,required:true}}},
							{field : 'FJDJS',title : '附件单据数', width : 100, align : 'center',editor:'text'},
							{field : 'ZHAIYAO',title : '摘要',width : 120,align : 'center',editor:'text'},
					        {field : 'KMBH',title : '科目编码',width : 120, editor:{type:'combobox',options:{valueField:'KMBH',textField:'KMMC',data:kmbm,required:true}}},
	                        {field : 'KMMC',title : '科目名称',width : 150,align : 'center'},
	                        {field : 'JFFSE',title : '借方发生额',width : 100,align : 'center',editor:'text'},
	                        {field : 'DFFSE',title : '贷方发生额',width : 100,align : 'center',editor:'text'},
	                        {field:'action',title:'操作',width:80,align:'center', formatter:function(value,row,index){
								var str = '';
								str += $.formatString('<a href="#"  onClick="deleteFun('+row.ID+') ">删除</a>');
							str += '&nbsp;';
							return str;
						}
					}
	                        
					]],
					onLoadSuccess : function() {
						$('#searchForm table').show();
						parent.$.messager.progress('close');
						$(this).datagrid('tooltip');
					}
				});
				dataGrid.datagrid('resize');
			}
			
			function add(){
 				var psel = document.getElementById("XSPZH");
			  	var editIndex = $('#dataGridxyfl').datagrid('getRowIndex', $('#dataGridxyfl').datagrid('getSelected'));
			  	$('#dataGridxyfl').datagrid('endEdit', editIndex);
			  	var insertRows = $('#dataGridxyfl').datagrid('getChanges');
	    		var changesRows = {
	    	    		inserted : []
	    	    	};
				   	if (insertRows.length>0) {
					   	for (var i=0;i<insertRows.length;i++) {
					   		changesRows.inserted.push(insertRows[i]);
					  	}
				   	}
	
				$.ajax({
				 	 type: "post",   
				     url: "${pageContext.request.contextPath}/business/accounting/xYRJZ/bgadd.shtml?xspzh="+psel.value,
				     data: "inserted="+JSON.stringify(changesRows.inserted),   
				 	 datatype: "json",
				 	 success:function(data){
				console.info(data);
				dataGrid.datagrid('load', {});
				         }
				    })
			}
			
			function insert(){
				var row = $('#dataGridxyfl').datagrid('getSelected');
				if (row){
					var index = $('#dataGridxyfl').datagrid('getRowIndex', row);
				} else {
					index = 0;
				}
				$('#dataGridxyfl').datagrid('insertRow', {
					index: index,
					row:{
						status:'P'
					}
				});
				$('#dataGridxyfl').datagrid('selectRow',index);
				$('#dataGridxyfl').datagrid('beginEdit',index);
			}
			
			function deleteFun(id){
					parent.$.messager.confirm('询问', '您是否要删除当前选择数据？', function(b) {
					if (b) {
						parent.$.messager.progress({
							title : '提示',
							text : '数据处理中，请稍后....'
						});
						$.post('${pageContext.request.contextPath}/business/accounting/xYRJZ/pzfldelete.shtml', {
							id : id
						}, function(result) {
							if (result.success) {
								parent.$.messager.alert('提示', result.msg, 'info');
								dataGrid.datagrid('reload');
							}
							parent.$.messager.progress('close');
						}, 'JSON');
					}
				});
			}
				    
		</script>
	
		<div class="text clearfix" style="text-align: center;">
			<div>&nbsp;&nbsp;</div>
			<span style="algin: center"> 
				<a class="l-btn" id="reset_button"><span class="l-btn-left"><span class="l-btn-text" onclick="insert()">新增</span></span></a>
					&nbsp;&nbsp;
				<a  onclick="add()"   class="l-btn" id="add_button"><span class="l-btn-left"><span class="l-btn-text">存盘</span></span></a>
			</span>
			<div>&nbsp;&nbsp;</div>
		</div>
	
		<div class="easyui-layout" data-options="fit : true,border : false">
				<form id="searchForm">
					<input id='XSPZH' name="XSPZH" type="hidden" class="span2" style="width:200px; height: 27px;display:none" value="${gb.XSPZH}"  class="form_view_input combo easyui-combobox" />
				</form>
			</div>
			<div data-options="region:'center',border:false">
				<table id="dataGridxyfl"></table>
			</div>
	</body>
</html>